/**
 * AreaService.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2013年10月10日
 */
package com.pshare.artemis.basic.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pshare.framework.page.PageInfo;
import com.pshare.framework.query.QueryInfo;
import com.pshare.artemis.basic.bean.City;
import com.pshare.artemis.basic.bean.District;
import com.pshare.artemis.basic.bean.Landmark;
import com.pshare.artemis.basic.bean.Province;
import com.pshare.tool.StaticMapStore;

@Service
@Transactional
public class AreaService {

	private static final Logger logger = Logger.getLogger(AreaService.class);
	private static StaticMapStore store = StaticMapStore.getInstance();
	
	@Autowired
	private ProvinceService provinceService;
	@Autowired
	private CityService cityService;
	@Autowired
	private DistrictService districtService;
	@Autowired
	private LandmarkService landmarkService;

	//@PostConstruct
	private void init() {
		initProvince();
	}

	@PreDestroy
	public void destory() {
		logger.info("area service destroy");
	}

	public List<Province> getAllProvinces() {
		return provinceService.queryAll();
	}

	public List<City> getAllCities() {
		return cityService.queryAll();
	}

	public List<District> getAllDistricts() {
		return districtService.queryAll();
	}

	/**
	 * 根据省份ID读取城市ID
	 * @param provinceId
	 * @return List<City>
	 */
	public List<City> getCitiesByProvinceId(Integer provinceId) {
		if (provinceId == null) {
			return new ArrayList<City>();
		}
		return cityService.getCitiesByProvinceId(provinceId);
	}
	
	public String getProinceName(Integer provinceId) {
		if (provinceId == null) {
			return "";
		}
		return provinceService.getProinceName(provinceId);
	}

	public String getCityName(Integer cityId) {
		if (cityId == null) {
			return "";
		}
		return cityService.getCityName(cityId);
	}

	public String getDistrictName(Integer districtId) {
		if (districtId == null) {
			return "";
		}
		return districtService.getDistrictName(districtId);
	}
	
	/**
	 * 根据城市名称分页模糊查找城市
	 * @param cityName
	 * @return PageInfo<City>
	 */
	public PageInfo<City> queryCity(String cityName) {
		if (StringUtils.isBlank(cityName)) {
			return new PageInfo<City>();
		}
		QueryInfo queryInfo = new QueryInfo();
		queryInfo.addParams("name", cityName);
		return cityService.queryList(queryInfo);
	}

	private void initProvince() {
		logger.info("开始加载全国省市区县数据...");
		long start = System.currentTimeMillis();

		boolean psFlag = store.containsKey(StaticMapStore.Key.PROVINCE);
		if (!psFlag) {
			List<Province> psList = provinceService.queryAll();
			store.put(StaticMapStore.Key.PROVINCE, psList);
			store.put(StaticMapStore.Key.PROVINCEMAP, getProvinceMap(psList));
			logger.info("加载省级数据完成，共：" + psList.size() + "条数据...");
		}

		boolean csFlag = store.containsKey(StaticMapStore.Key.CITY);
		if (!csFlag) {
			List<City> csList = cityService.queryAll();
			store.put(StaticMapStore.Key.CITY, csList);
			store.put(StaticMapStore.Key.CITYMAP, getCityMap(csList));
			logger.info("加载市级数据完成，共：" + csList.size() + "条数据...");
		}

		boolean dsFlag = store.containsKey(StaticMapStore.Key.DISTRICT);
		if (!dsFlag) {
			List<District> dsList = districtService.queryAll();
			store.put(StaticMapStore.Key.DISTRICT, dsList);
			store.put(StaticMapStore.Key.DISTRICTMAP, getDistrictMap(dsList));
			logger.info("加载区县级数据完成，共：" + dsList.size() + "条数据...");
		}
		logger.info("加载全国省市区县数据完成...");
		logger.info("cost: " + (System.currentTimeMillis() - start));
		
		boolean lmFlag = store.containsKey(StaticMapStore.Key.LANDMARKMAP);
		if (!lmFlag) {
			Map<Integer, List<Landmark>> lmMap = new HashMap<Integer, List<Landmark>>();
			store.put(StaticMapStore.Key.LANDMARKMAP, lmMap);
		}
	}

	private Map<Integer, String> getProvinceMap(List<Province> psList) {
		Map<Integer, String> psMap = new HashMap<Integer, String>();
		for (Province p : psList) {
			psMap.put(p.getId(), p.getName());
		}
		return psMap;
	}

	private Map<Integer, String> getCityMap(List<City> csList) {
		Map<Integer, String> csMap = new HashMap<Integer, String>();
		for (City c : csList) {
			csMap.put(c.getId(), c.getName());
		}
		return csMap;
	}

	private Map<Integer, String> getDistrictMap(List<District> dsList) {
		Map<Integer, String> dsMap = new HashMap<Integer, String>();
		for (District d : dsList) {
			dsMap.put(d.getId(), d.getName());
		}
		return dsMap;
	}
	
	public List<Landmark> getLandMarks(Integer districtId) {
		List<Landmark> landmarks = null;
		if (districtId == null) {
			return landmarks;
		}
		return landmarkService.getLandmarks(districtId);
	}
	
	public String getCityCode(Integer cityId) {
		return cityService.getCityCode(cityId);
	}
	
	public String getFullAddr(Integer provinceId, Integer cityId, Integer districtId) {
		String addr = "";
		if (cityId == null && districtId != null) {
			cityId = districtService.getCityIdByDistrictId(districtId);
		}
		if (provinceId == null && cityId != null) {
			provinceId = cityService.getProvinceIdByCityId(cityId);
		}
		
		if (provinceId != null) {
			addr = provinceService.getProinceName(provinceId);
		}
		if (cityId != null) {
			addr += cityService.getCityName(cityId);
		}
		if (districtId != null) {
			addr += districtService.getDistrictName(districtId);
		}
		return addr;
	}

}
